package com.sh.personnel.config.init;

import com.sh.personnel.config.redis.RedisConstant;
import com.sh.personnel.mapper.InitMapper;
import com.sh.personnel.util.RandomUtil;
import com.sh.personnel.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 获取流水工号
 * @author 林思浩
 * @date 2020/03/12 13:58
 */
@Order(1)
@Component
public class MyCommandLineRunner implements CommandLineRunner {

    @Autowired
    InitMapper initMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("初始化流水工号..");
        String initUsername = initMapper.getMaxUsername();
        if(initUsername != null){
            if((initUsername.substring(0, 6)).equals(usernamePrefix())) {
                redisTemplate.opsForValue().set(RedisConstant.INIT_USERNAME, initUsername);
                return;
            }
        }
        initUsername = usernamePrefix() + "0001";
        redisTemplate.opsForValue().set(RedisConstant.INIT_USERNAME, initUsername);
    }

    private String usernamePrefix() {
        return TimeUtil.currentYear() + TimeUtil.currentMonth();
    }

}
